﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// 文件名：ClsInvoiceVerificationDAL.cs
/// 作  者：SAM
/// 时  间：2006.12.4
/// 说  明：ClsInvoiceVerificationDAL类用于票据核销信息相关处理
/// </summary>
public class ClsInvoiceVerificationDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有主表信息
    /// </summary>
    /// <param name="Type">类型</param>
    /// <returns>返回主表信息数据集</returns>
    public DataSet GetInvoiceVerificationMains()
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetInvoiceVerificationMains", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        //dat.SelectCommand.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        //dat.SelectCommand.Parameters["@type"].Value = Type;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 获取所有从表信息
    /// <param name="Dm_No">收料或退货单号</param>
    /// <param name="Type">类型</param>
    /// </summary>
    /// <returns>返回从表信息数据集</returns>
    public DataSet GetInvoiceVerificationChilds(string Dm_No, string Type)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetStockOrderChilds", con);//Pro_GetInvoiceVerificationChilds

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        dat.SelectCommand.Parameters["@dm_no"].Value = Dm_No;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        dat.SelectCommand.Parameters["@type"].Value = Type;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 改变订单的状态
    /// </summary>
    /// <param name="Dm_No">订单单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Auditor">操作员</param>
    /// <param name="Type">类型</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdateInvoiceVerificationState(string Dm_No, int State, string Auditor, string Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdateInvoiceVerificationState", con);//Pro_ChangeStore

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = Dm_No;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = State;
        //com.Parameters.Add("@AuditState", SqlDbType.Int, 4);
        //com.Parameters["@AuditState"].Value = State;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 改变订单从表的状态
    /// </summary>
    /// <param name="Dm_No">订单单号</param>
    /// <param name="N_No">从表单号</param>
    /// <param name="State">操作状态(0确认，1取消确认即终止，2作废即作废，3彻底作废)</param>
    /// <param name="Type">类型</param>
    /// <returns>操作是否成功</returns>
    public Boolean UpdateInvoiceVerificationChildState(string Dm_No, string N_No, int State, string Type)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpdateInvoiceVerificationChildState", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = Dm_No;
        com.Parameters.Add("@n_no", SqlDbType.NVarChar, 30);
        com.Parameters["@n_no"].Value = N_No;
        com.Parameters.Add("@dm_state", SqlDbType.Int, 4);
        com.Parameters["@dm_state"].Value = State;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = Type;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 新增或修改主表记录
    /// </summary>
    /// <param name="Type">类型</param>
    /// <param name="Dm_No">单号</param>
    /// <param name="Dm_Date">日期</param>
    /// <param name="Dm_Mdate">希望发货日(退货为当前系统时间)</param>
    /// <param name="Ct_Id">供应商ID</param>
    /// <param name="Pm_Id">付款方式ID(退货为空)</param>
    /// <param name="Sm_Id">运输方式ID(退货为空)</param>
    /// <param name="Wh_Id">仓库ID</param>
    /// <param name="Dp_Id">部门ID</param>
    /// <param name="Wk_Id">采购员ID</param>
    /// <param name="Dm_Name">收货人(退货为空)</param>
    /// <param name="Dm_Tel">联系电话(退货为空)</param>
    /// <param name="Dm_Addr">收货地址(退货为空)</param>
    /// <param name="Oper">录入人</param>
    /// <param name="Remark">备注</param>
    /// <param name="OperateType">操作类型(0新增，1修改)</param>
    /// <returns>返回操作是否成功</returns>
    /// strDm_type,strVm_no,dtMoDate,strMoNo,strDm_No,strBk_id,strBacc_no,strOper,strRemark
    public Boolean AddInvoiceVerification(string sDm_type, string sVm_no, DateTime dMoDate, string sMoNo, string sDm_No, string sBk_id, string sBacc_no,
                                             string sOper,string sRemark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddInvoiceVerification", con);
        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add("@dm_type", SqlDbType.NVarChar, 2);
        com.Parameters["@dm_type"].Value = sDm_type;
        com.Parameters.Add("@vm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@vm_no"].Value = sVm_no;
        com.Parameters.Add("@vm_date", SqlDbType.DateTime, 8);
        com.Parameters["@vm_date"].Value = dMoDate;

        com.Parameters.Add("@v_no", SqlDbType.NVarChar, 30);
        com.Parameters["@v_no"].Value = sMoNo;
        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = sDm_No;
        com.Parameters.Add("@bk_id", SqlDbType.NVarChar, 30);
        com.Parameters["@bk_id"].Value = sBk_id;
        com.Parameters.Add("@bacc_no", SqlDbType.NVarChar, 30);
        com.Parameters["@bacc_no"].Value = sBacc_no;
        com.Parameters.Add("@oper", SqlDbType.NVarChar, 30);
        com.Parameters["@oper"].Value = sOper;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = sRemark;
        //com.Parameters.Add("@operate_type", SqlDbType.Int, 4);
        //com.Parameters["@operate_type"].Value = OperateType;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 新增或修改从表记录
    /// </summary>
    /// <param name="Type">类型</param>
    /// <param name="Dm_No">采购订单号</param>
    /// <param name="N_No">从表单号</param>
    /// <param name="Im_No">商品编号</param>
    /// <param name="Qty">数量</param>
    /// <param name="Agio">折扣率</param>
    /// <param name="Remark">备注</param>
    /// <param name="OperateType">操作类型(0新增，1修改)</param>
    /// <returns>返回操作是否成功</returns>
    public Boolean AddOrUpdateInvoiceVerificationChild(string Type, string Dm_No, string N_No, string Im_No, decimal Qty, decimal Agio, string Remark, int OperateType)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddOrUpdateInvoiceVerificationChild", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@type", SqlDbType.NVarChar, 2);
        com.Parameters["@type"].Value = Type;
        com.Parameters.Add("@dm_no", SqlDbType.NVarChar, 30);
        com.Parameters["@dm_no"].Value = Dm_No;
        com.Parameters.Add("@n_no", SqlDbType.NVarChar, 30);
        com.Parameters["@n_no"].Value = N_No;
        com.Parameters.Add("@im_no", SqlDbType.NVarChar, 30);
        com.Parameters["@im_no"].Value = Im_No;
        com.Parameters.Add("@qty", SqlDbType.Decimal, 17);
        com.Parameters["@qty"].Value = Qty;
        com.Parameters.Add("@agio", SqlDbType.Decimal, 28);
        com.Parameters["@agio"].Value = Agio;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;
        com.Parameters.Add("@operate_type", SqlDbType.Int, 4);
        com.Parameters["@operate_type"].Value = OperateType;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }



    public DataSet GetStockMains(string aa, string bb, string cc)
    {
        return null;
    }
    public DataSet GetStockChilds(string Dm_No, string Type)
    {
        return null;
    }
}
